/**
 * 一个镜子的阵列，要求光线向右射进（0，0），并且向右射出（N-1, M-1）
 * 问最少要改变多少个格子，才能实现目标
 * 注意到光线是可以迂回的，因此不能够按照 for ++i for ++j 的顺序进行处理
 * 需要用优先级队列
 * 
 * 令 Dijo 为从起点到ij且出射方向为o需要改动的最少次数
 * 则使用类Dij的刷表法，已知Dijo，则有可能影响三个量
 * 例如如果o==RIGHT，则会影响D[i][j+1][上、下、右]
 * 再根据A[i][j+1]的原有内容可知可能会影响到的取值
 * 
 */
#include <bits/stdc++.h>
using namespace std;

using llt = long long;
using vi = vector<int>;
using vvi = vector<vi>;

// 出射方向
int const UP = 0;
int const RIGHT = 1;
int const DOWN = 2;
int const LEFT = 3; 

struct _t{
    int row;
    int col;
    int orient;
    int d;
    _t() = default;
    _t(int r, int c, int o, int dd):row(r),col(c),orient(o),d(dd){}
    bool operator < (const _t & rhs) const {
        if(d != rhs.d) return d > rhs.d;
        if(row != rhs.row) return row > rhs.row;
        if(col != rhs.col) return col > rhs.col;
        return orient > rhs.orient;
    }
};

int N, M;
vector<string> Board;
vector<vvi> D;
priority_queue<_t> Q;
vector<vector<vector<bool>>> Flag;

void qpush(const _t & t){
    Q.push({t.row, t.col, t.orient, D[t.row][t.col][t.orient] = t.d});
}

bool ismin(int d, int a){
    return -1 == d or a < d;
}

void chkpush(const _t & t){
    if(ismin(D[t.row][t.col][t.orient], t.d)){
        Q.push({t.row, t.col, t.orient, D[t.row][t.col][t.orient] = t.d});
    }
}

void work(){
    while(not Q.empty()) Q.pop();

    cin >> N >> M;
    Board.assign(N, {});
    for(auto & s : Board) cin >> s;
    D.assign(N, vvi(M, vi(4, -1)));
    Flag.assign(N, vector<vector<bool>>(M, vector<bool>(4, 0)));

    switch(Board[0][0]){
        case 'A': {
            qpush({0, 0, RIGHT, 0});
            Q.push({0, 0, DOWN,  D[0][0][DOWN] = 1});
            Q.push({0, 0, UP, D[0][0][UP] = 1});
            break;
        }
        case 'B': {
            Q.push({0, 0, DOWN, D[0][0][DOWN] = 0});
            Q.push({0, 0, RIGHT, D[0][0][RIGHT] = 1});
            Q.push({0, 0, UP, D[0][0][UP] = 1});
            break;
        }
        case 'C': {
            Q.push({0, 0, UP, D[0][0][UP] = 0});
            Q.push({0, 0, RIGHT, D[0][0][RIGHT] = 1});
            Q.push({0, 0, DOWN, D[0][0][DOWN] = 1});
            break;
        }
        default: assert(0);
    }    

    while(1){
        _t h;
        while(not Q.empty()){
            h = Q.top(); 
            if(not Flag[h.row][h.col][h.orient]) break;
            else Q.pop();
        }
        if(Q.empty()) break;
        Q.pop();

        Flag[h.row][h.col][h.orient] = true;
        if(h.row + 1 == N and h.col + 1 == M and h.orient == RIGHT){
            return (void)(cout << h.d << "\n");
        }
        switch(h.orient){
            case UP:{
                if(h.row > 0){
                    auto & d = D[h.row - 1][h.col];
                    switch(Board[h.row - 1][h.col]){
                        case 'A':{
                            if(ismin(d[UP], h.d)){
                                qpush({h.row - 1, h.col, UP, h.d});
                            }
                            if(ismin(d[LEFT], h.d + 1)){
                                qpush({h.row - 1, h.col, LEFT, h.d + 1});
                            }
                            if(ismin(d[RIGHT], h.d + 1)){
                                qpush({h.row - 1, h.col, RIGHT, h.d + 1});
                            }
                            break;
                        }
                        case 'B':{
                            if(ismin(d[LEFT], h.d)){
                                qpush({h.row - 1, h.col, LEFT, h.d});
                            }
                            if(ismin(d[UP], h.d + 1)){
                                qpush({h.row - 1, h.col, UP, h.d + 1});
                            }
                            if(ismin(d[RIGHT], h.d + 1)){
                                qpush({h.row - 1, h.col, RIGHT, h.d + 1});
                            }
                            break;
                        }
                        case 'C':{
                            if(ismin(d[LEFT], h.d + 1)){
                                qpush({h.row - 1, h.col, LEFT, h.d + 1});
                            }
                            if(ismin(d[UP], h.d + 1)){
                                qpush({h.row - 1, h.col, UP, h.d + 1});
                            }
                            if(ismin(d[RIGHT], h.d)){
                                qpush({h.row - 1, h.col, RIGHT, h.d});
                            }
                            break;
                        }
                    }
                }
                break;
            }
            case DOWN: {
                if(h.row + 1 < N){
                    auto & d = D[h.row + 1][h.col];
                    switch(Board[h.row + 1][h.col]){
                        case 'A':{
                            if(ismin(d[DOWN], h.d)){
                                qpush({h.row + 1, h.col, DOWN, h.d});
                            }
                            if(ismin(d[LEFT], h.d + 1)){
                                qpush({h.row + 1, h.col, LEFT, h.d + 1});
                            }
                            if(ismin(d[RIGHT], h.d + 1)){
                                qpush({h.row + 1, h.col, RIGHT, h.d + 1});
                            }
                            break;
                        }
                        case 'B':{
                            if(ismin(d[DOWN], h.d + 1)){
                                qpush({h.row + 1, h.col, DOWN, h.d + 1});
                            }
                            if(ismin(d[LEFT], h.d + 1)){
                                qpush({h.row + 1, h.col, LEFT, h.d + 1});
                            }
                            if(ismin(d[RIGHT], h.d)){
                                qpush({h.row + 1, h.col, RIGHT, h.d});
                            }
                            break;
                        }
                        case 'C':{
                            chkpush({h.row + 1, h.col, LEFT, h.d});
                            chkpush({h.row + 1, h.col, RIGHT, h.d + 1});
                            chkpush({h.row + 1, h.col, DOWN, h.d + 1});
                            break;
                        }
                    }
                }
                break;
            }
            case LEFT: {
                if(h.col > 0){
                    auto & d = D[h.row][h.col - 1];
                    switch(Board[h.row][h.col - 1]){
                        case 'A':{
                            chkpush({h.row, h.col - 1, LEFT, h.d});
                            chkpush({h.row, h.col - 1, UP, h.d + 1});
                            chkpush({h.row, h.col - 1, DOWN, h.d + 1});
                        }break;
                        case 'B':{
                            chkpush({h.row, h.col - 1, LEFT, h.d + 1});
                            chkpush({h.row, h.col - 1, UP, h.d});
                            chkpush({h.row, h.col - 1, DOWN, h.d + 1});
                        }break;
                        case 'C':{
                            chkpush({h.row, h.col - 1, LEFT, h.d + 1});
                            chkpush({h.row, h.col - 1, UP, h.d + 1});
                            chkpush({h.row, h.col - 1, DOWN, h.d});
                            break;
                        }
                    }
                }
                break;
            } 
            case RIGHT:{
                if(h.col + 1 < M){
                    switch(Board[h.row][h.col + 1]){
                        case 'A':{
                            chkpush({h.row, h.col + 1, RIGHT, h.d});
                            chkpush({h.row, h.col + 1, UP, h.d + 1});
                            chkpush({h.row, h.col + 1, DOWN, h.d + 1});
                        }break;
                        case 'B':{
                            chkpush({h.row, h.col + 1, RIGHT, h.d + 1});
                            chkpush({h.row, h.col + 1, UP, h.d + 1});
                            chkpush({h.row, h.col + 1, DOWN, h.d});
                        }break;
                        case 'C':{
                            chkpush({h.row, h.col + 1, RIGHT, h.d + 1});
                            chkpush({h.row, h.col + 1, UP, h.d});
                            chkpush({h.row, h.col + 1, DOWN, h.d + 1});
                        }break;
                    }
                }
                break;
            }
        }

    }

    return;
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("z.txt", "r", stdin);
#endif
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);	
    int nofkase = 1;
    cin >> nofkase;
    while(nofkase--) work();
	return 0;
}